package zhang.lao.api.controller.app;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.dao.base.WnCompanyDao;
import zhang.lao.dao.base.WnFarmDao;
import zhang.lao.dao.base.WnMachineDao;
import zhang.lao.dao.base.WnMachineToolDao;

import javax.annotation.Resource;

/**
 * @author
 * @create 2018-03-16 10:49
 **/
@Controller
public class DownController {

    @Resource
    private WnMachineDao wnMachineDao;
    @Resource
    private WnMachineToolDao wnMachineToolDao;
    @Resource
    private WnFarmDao wnFarmDao;
    @Resource
    private WnCompanyDao wnCompanyDao;
    @RequestMapping("/qrcode/mechine/{machineNumber}")
        public String getMachine(@PathVariable String machineNumber, ModelMap modelMap){
        WnMachineExample wnMachineExample = new WnMachineExample();
        wnMachineExample.createCriteria().andMachineNumberEqualTo(machineNumber);
        WnMachine first = ListUtils.getFirst(wnMachineDao.selectByExample(wnMachineExample));
        if(first!=null) {
            modelMap.put("machineNumber",first.getMachineNumber());
            modelMap.put("licensePlateNumber",first.getLicensePlateNumber());
            modelMap.put("brand",first.getBrand());
            modelMap.put("modelNumber",first.getModelNumber());
            String farmId = first.getFarmId();
            WnFarm wnFarm = wnFarmDao.selectByPrimaryKey(farmId);
            if(wnFarm!=null){
                modelMap.put("farmName",wnFarm.getName());
                WnCompany wnCompany = wnCompanyDao.selectByPrimaryKey(wnFarm.getCompanyId());
                if(wnCompany!=null){
                    modelMap.put("companyName",wnCompany.getName());
                }
            }
        }
        modelMap.put("downLoadUrl","http://sucai.file.alimmdn.com/apk/20180316.apk?t=1521169491087");
        return "machine/machine";
        }

    @RequestMapping("/qrcode/mechine/tool/{toolNumber}")
    public String toolNumber(@PathVariable String toolNumber,ModelMap modelMap){
        WnMachineToolExample wnMachineExample = new WnMachineToolExample();
        wnMachineExample.createCriteria().andToolNumberEqualTo(toolNumber);
        WnMachineTool first = ListUtils.getFirst(wnMachineToolDao.selectByExample(wnMachineExample));
        if(first!=null) {
            modelMap.put("machineNumber",first.getToolNumber());
            modelMap.put("type",first.getType());
            modelMap.put("breadth",first.getBreadth());
            String farmId = first.getFarmId();
            WnFarm wnFarm = wnFarmDao.selectByPrimaryKey(farmId);
            if(wnFarm!=null){
                modelMap.put("farmName",wnFarm.getName());
                WnCompany wnCompany = wnCompanyDao.selectByPrimaryKey(wnFarm.getCompanyId());
                if(wnCompany!=null){
                    modelMap.put("companyName",wnCompany.getName());
                }
            }
        }
        modelMap.put("downLoadUrl","http://sucai.file.alimmdn.com/apk/20180316.apk?t=1521169491087");
        return "machine/machineTool";
    }


}
